//
//  AppDelegate.swift
//  RLocaLink
//
//  Created by Icy on 2017/4/3.
//  Copyright © 2017年 Icy. All rights reserved.
//

import UIKit
import URLNavigator
import UtilCore
import SwiftyUserDefaults
import IQKeyboardManager
import ModelProtocol
import NetWorkCore
import GoogleMaps


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.main.bounds);
        self.window!.backgroundColor=UIColor.white;
        //        setupURLNavigator(launchOptions)   //注册app路由
        IQKeyboardManager.shared().isEnabled = true
        IQKeyboardManager.shared().toolbarDoneBarButtonItemText = "完成"
        setCommonAppleance()      //设置导航栏颜色
         GMSServices.provideAPIKey(googleMapAppKey)
        setupURLNavigator(launchOptions) //App 路由配置
        setNetCoreConfing()
        setMDManager()
        setupRootViewController()
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}
/// 基础配置
extension AppDelegate{
    internal func setupRootViewController() {
        let  firstPage = Defaults[.firstPage]
        /// 登陆成功后 点击了进入觅客商城按钮 则设置为true 否则为false
        let  hasLogined = Defaults[.hasLogined]
        let user = LoginedUser.sharedInstance.user
        ///表示第一次登陆
        if firstPage == false {
            let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
            
            window?.rootViewController =  UINavigationController(rootViewController: storyboard.instantiateViewController(withIdentifier: "Development_Map_Vc"))
            self.window?.makeKeyAndVisible()
        }else if user != nil && hasLogined{
            self.setTabBarVc()
            
        }else{
            self.toLoginVc()
        }
    }
    /// 设置tabbarcontroller
    internal func setTabBarVc() ->  Void{
        let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
        
        let tabbar0 = UITabBarItem(title: "HOME", image:UIImage(named: "tab-unselected-home") ,selectedImage:UIImage(named: "tab-unselected-home"))
        let tabbar1 = UITabBarItem(title: "WORK", image:UIImage(named: "tab-unselected-word") ,selectedImage:UIImage(named: "tab-selected-word"))
        let tabbar2 = UITabBarItem(title: "LINK", image:UIImage(named: "tab-unselected-lind") ,selectedImage:UIImage(named: "tab-selected-lind"))
        let tabbar3 = UITabBarItem(title: "SETTING", image:UIImage(named: "tab-unselected-setting") ,selectedImage:UIImage(named: "tab-selected-setting"))
        
        let nav0 = UINavigationController(rootViewController: storyboard.instantiateViewController(withIdentifier: "Home_Vc"))
        nav0.tabBarItem = tabbar0
        let nav1 = UINavigationController(rootViewController:  storyboard.instantiateViewController(withIdentifier: "Work_Vc"))
        nav1.tabBarItem = tabbar1
        let nav2 = UINavigationController(rootViewController:  storyboard.instantiateViewController(withIdentifier: "Link_Vc"))
        nav2.tabBarItem = tabbar2
        let nav3 = UINavigationController(rootViewController:  storyboard.instantiateViewController(withIdentifier: "Settings_Vc"))
        nav3.tabBarItem = tabbar3
        let tabBar=UITabBarController()
        tabBar.viewControllers=[nav0,nav1,nav2,nav3]
        
        self.window?.rootViewController=tabBar
    }
    /**
     登录成功跳转到Development_Map_Vc
     */
    internal func toMapVc(){
        let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
        let mapVc =  storyboard.instantiateViewController(withIdentifier:"Development_Map_Vc")
        let naVc = UINavigationController(rootViewController: mapVc)
        window!.rootViewController = naVc
    }
    /**
     跳转到登陆界面
     */
    internal func toLoginVc(){
//        let loginNav = UINavigationController(rootViewController: JonSnowCore.loginVc)
//        window!.rootViewController = loginNav
    }
    /**
     设置导航等 的主题颜色
     */
    internal func setCommonAppleance(){
        /// nav
        UIApplication.shared.statusBarStyle = .lightContent
        let navigationBarAppearance = UINavigationBar.appearance()
        //设置显示的颜色
        navigationBarAppearance.barTintColor = UIColor.basic()
        navigationBarAppearance.tintColor = UIColor.white
        
        /// tabbar
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
        let tabBarAppearance = UITabBar.appearance()
        tabBarAppearance.tintColor = UIColor.basic()
        tabBarAppearance.barTintColor = UIColor.white
        tabBarAppearance.isOpaque = true
    }
}
extension AppDelegate{
    ///app路由注册
    internal func setupURLNavigator(_ launchOptions: [UIApplicationLaunchOptionsKey: Any]?){
        Navigator.scheme = "RLocaLink"
        MainNavigatorMap.initialize()
        
        if let url = launchOptions {
            
            print(url)
        }
    }
    /// 修改NetWorkCore 网络相关配置
    internal func setNetCoreConfing() {
        ///这块主要是图片封装的时候回用到图片地址
        UtilCore.sharedInstance.baseUrl = IMG_BASEURL
        NetWorkCore.baseUrl = BASEURL

    }
    func setMDManager() {
        //始化蓝牙状态
        
        MDManager.shared().initBlue()
        MDManager.shared().setAppKey(appKey)
        
    }
}
